#!/bin/sh

# Set mdev
echo /sbin/mdev > /proc/sys/kernel/hotplug
/sbin/mdev -s && echo "mdev is ok......"

# networking
ifconfig lo up

mount -t squashfs /dev/mtdblock3 /appfs
mount -t squashfs /dev/mtdblock4 /audio_file

ubiattach -m 5 -d 5 /dev/ubi_ctrl
if [ $? -eq 0 ];then
    echo "exit ubifs"
    mount -t ubifs -o sync /dev/ubi5_0 /mnt/config/
else
    echo "no ubifs"
    ubiformat /dev/mtd5 -y
    ubiattach -m 5 -d 5 /dev/ubi_ctrl
    ubimkvol /dev/ubi5 -N config -m
    mount -t ubifs -o sync /dev/ubi5_0 /mnt/config/
fi

. /etc/common.env
. /etc/chip.env
. /etc/profile.env
insmod_sdk_modules.sh
read_sensor_id.sh
. /etc/sensor_type.env

# enable wifi gpio #
echo $G_USB_WIFI_GPIO > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio$G_USB_WIFI_GPIO/direction
echo 0 > /sys/class/gpio/gpio$G_USB_WIFI_GPIO/value

read_wifi_module.sh
. /etc/wifi_type.env

if [ $G_WIFI_MODULE = "atbm6x3x_wifi_usb" ];then
    insmod /lib/modules/$G_WIFI_MODULE.ko wifi_bt_comb=1
else
    insmod /lib/modules/$G_WIFI_MODULE.ko stacfgpath=/etc/ssv6x5x-wifi.cfg
fi

# upload jpg path #
mkdir /tmp/tmpfs

telnetd &
sdk_tcp_daemon.sh &

if [ -e "/mnt/config/audio_record_file.pcm" ];then
    size=`stat -c %s /mnt/config/audio_record_file.pcm`
    if [ $size -ge 204800 ];then
        echo "record pcm is too big"
        rm /mnt/config/audio_record_file.pcm
    fi
fi

# Run init script
if [ -f /system/init/app_init.sh ]; then
    /system/init/app_init.sh &
fi
